import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_page/home_business/search_page/widget/search_histoty_view.dart';
import 'package:xvideos/tools_base/indicator/custom_tab_indicator.dart';
import 'package:xvideos/hj_utils/screen.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:provider/provider.dart';

import '../video_all_type_page.dart';

final searchTextController = TextEditingController();

final class SearchAppBar extends StatefulWidget {
  final Function(String keywork) onSubmitted;
  SearchAppBar({super.key, required this.onSubmitted});

  @override
  State<SearchAppBar> createState() => _SearchAppBarState();
}

class _SearchAppBarState extends State<SearchAppBar> {

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<TextEditingController>(
      builder: (_, ctr, __) {
        return Container(
          padding: EdgeInsets.only(top: screen.paddingTop),
          color: Colors.black,
          child: Container(
            height: 56,
            padding: EdgeInsets.symmetric(horizontal: 12),
            child: Row(
              children: [
                GestureDetector(
                  onTap: () => Get.back(),
                  child: Image.asset(
                    'common_back.png'.commonImgPath,
                    width: 22,
                    height: 22,
                  ),
                ),
                10.sizeBoxW,
                Expanded(
                  child: Container(
                    height: 32,
                    padding: EdgeInsets.symmetric(horizontal: 10),
                    alignment: Alignment.centerLeft,
                    decoration: BoxDecoration(color: Color(0x11FFFFFF), borderRadius: BorderRadius.circular(20)),
                    child: Row(
                      children: [
                        InkWell(
                          onTap: (){
                            Get.to(() => VideoAllTypePage(), opaque: false);
                          },
                          child: Row(
                            children: [
                              2.sizeBoxW,
                              Image.asset('libary_search.webp'.homePath, width: 20),
                              6.sizeBoxW,
                              Text(
                                '片库',
                                style: TextStyle(color: Colors.white.withOpacity(.9), fontSize: 14,),
                              ),
                              6.sizeBoxW,
                              Container(height: 12, width: 1, color: Colors.white.withOpacity(.05)),
                              6.sizeBoxW,
                            ],
                          ),
                        ),
                        Expanded(
                          child: TextField(
                            controller: ctr,
                            style: TextStyle(color: Colors.white.withOpacity(.8), fontSize: 14, height: 1),
                            onSubmitted: (value) {
                              widget.onSubmitted(value);
                              searchHistortyProvider.saveHistories(value);
                            },
                            textInputAction: TextInputAction.search,
                            decoration: InputDecoration(
                              hintText: '请输入搜索内容',
                              hintStyle: TextStyle(color: Colors.white, fontSize: 14, height: 1),
                              border: InputBorder.none,
                              isDense: true,
                              contentPadding: EdgeInsets.zero,
                            ),
                          ),
                        ),
                        12.sizeBoxW,
                        Visibility(
                          visible: ctr.text.length > 0,
                          child: GestureDetector(
                            onTap: () => ctr.clear(),
                            child: Image.asset(
                              'search_close.webp'.homePath,
                              width: 18,
                              height: 18,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                10.sizeBoxW,
                InkWell(
                  onTap: () {
                    widget.onSubmitted(ctr.text);
                    searchHistortyProvider.saveHistories(ctr.text);
                  },
                  child: Text(
                    "搜索",
                    style: TextStyle(
                      color: Color(0x73FFFFFF),
                      fontSize: 16,
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}

class SearchResultHader extends StatefulWidget {
  final TabController tabController;
  final List<String> tabs;
  const SearchResultHader({super.key, required this.tabController, required this.tabs});

  @override
  State<SearchResultHader> createState() => _SearchResultHaderState();
}

class _SearchResultHaderState extends State<SearchResultHader> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      // padding: EdgeInsets.symmetric(horizontal: 16),
      height: 36,
      child: TabBar(
        labelPadding: EdgeInsets.only(bottom: 3),
        padding: EdgeInsets.only(bottom: 6),
        labelStyle: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
        unselectedLabelStyle: TextStyle(color: Color(0x99ffffff), fontSize: 16),
        // tabAlignment: TabAlignment.start,
        // isScrollable: ,
        tabs: widget.tabs
            .map((e) => Container(
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  child: Text(e),
                ))
            .toList(),
        controller: widget.tabController,
        indicator: CustomIndicator(
          width: 16,
          height: 4,
          topLeft: 12,
          topRight: 12,
          bottomLeft: 12,
          bottomRight: 12,
          color: Color(0xff00D6BE),
        ),
      ),
    );
  }
}
